package org.csu.petstore.ssm.service.impl;

import org.csu.petstore.ssm.entity.Category;
import org.csu.petstore.ssm.entity.Item;
import org.csu.petstore.ssm.entity.Product;
import org.csu.petstore.ssm.persistence.CategoryMapper;
import org.csu.petstore.ssm.persistence.ItemMapper;
import org.csu.petstore.ssm.persistence.ProductMapper;
import org.csu.petstore.ssm.service.CatalogService;
import org.csu.petstore.ssm.vo.CategoryVO;
import org.csu.petstore.ssm.vo.ItemVO;
import org.csu.petstore.ssm.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public CategoryVO getCategory(String categoryId) {
        CategoryVO categoryVO = new CategoryVO();
        Category category = categoryMapper.getCategory(categoryId);

        List<Product> productList = productMapper.getProductListByCategory(categoryId);

        categoryVO.setProductList(productList);
        categoryVO.setCategoryId(categoryId);
        categoryVO.setCategoryName(category.getName());

        return categoryVO;
    }

    public List<Product> getProductListByCategory(String categoryId) {
       return productMapper.getProductListByCategory(categoryId);
    }

    @Override
    public ProductVO getProduct(String productId) {
        ProductVO productVO = new ProductVO();
        Product product = productMapper.getProduct(productId);
        List<Item> itemList = itemMapper.getItemListByProduct(productId);

        productVO.setProductId(productId);
        productVO.setProductName(product.getName());
        productVO.setCategoryId(product.getCategoryId());
        productVO.setItemList(itemList);
        return productVO;
    }

    @Override
    public ItemVO getItem(String itemId) {
        ItemVO itemVO = new ItemVO();
        Item item = itemMapper.getItem(itemId);
        Product product = productMapper.getProduct(item.getProductId());
        int quantity=itemMapper.getInventoryQuantity(itemId);


        itemVO.setItemId(itemId);
        itemVO.setListPrice(item.getListPrice());
        itemVO.setAttributes(item.getAttribute1());
        itemVO.setProductId(product.getProductId());
        itemVO.setProductName(product.getName());
        //itemVO.setDescription(product.getDescription());
        String [] temp = product.getDescription().split("\"");
        itemVO.setDescriptionImage(temp[1]);
        itemVO.setDescriptionText(temp[2].substring(1));

        itemVO.setQuantity(quantity);
        return itemVO;
    }

    public Item getItem2(String itemId) {
        return itemMapper.getItem(itemId);
    }

    @Override
    public CategoryVO getSearchCategory(String searchId) {
        CategoryVO categoryVO = new CategoryVO();

        List<Product> productList = productMapper.searchProductList(searchId);

        categoryVO.setProductList(productList);
        categoryVO.setCategoryName(searchId);

        return categoryVO;
    }

    public List<Product> searchProducts(String keyword) {
        return productMapper.searchProductList(keyword);
    }

    public boolean isItemInStock(String itemId) {
        return itemMapper.getInventoryQuantity(itemId) > 0;
    }
}
